package cn.lxh.template;

import cn.lxh.constants.Constants;
import cn.lxh.pojo.bo.PointBo;
import cn.lxh.pojo.result.DistanceMeasurementResult;
import cn.lxh.properties.TencentMapProperties;
import cn.lxh.utils.AssertUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @author lxh Create Time: 2023/12/30 13:06
 * Map工具类
 */
@Slf4j
public class MapTemplate {

    private TencentMapProperties tencentMapProperties;

    private RestTemplate restTemplate;

    public MapTemplate(TencentMapProperties tencentMapProperties, RestTemplate restTemplate) {
        this.tencentMapProperties = tencentMapProperties;
        this.restTemplate = restTemplate;
    }

    /**
     * 腾讯地图分段计算里程
     */
    public BigDecimal tencentMapSegmentedMileageCal(List<PointBo> points) {
        // 定义总里程
        BigDecimal totalMileage = BigDecimal.ZERO;
        // 遍历坐标集合
        for (int i = 0; i < points.size() - 1; i++) {
            // 获取当前坐标
            PointBo currentPoint = points.get(i);
            // 获取下一个坐标
            PointBo nextPoint = points.get(i + 1);
            // 开始测距
            DistanceMeasurementResult distanceMeasurementResult = tencentMapRanging(currentPoint.getLatitude(), currentPoint.getLongitude(),
                    nextPoint.getLatitude(), nextPoint.getLongitude(), Constants.Map.MODE_DRIVING);
            // 累加里程
            totalMileage = totalMileage.add(distanceMeasurementResult.getDistance());
        }
        return totalMileage;
    }

    /**
     * 腾讯地图测距
     */
    public DistanceMeasurementResult tencentMapRanging(String startLatitude, String startLongitude,
                                  String endLatitude, String endLongitude,
                                  String mode) {
        // 起点坐标
        String from = startLatitude + "," + startLongitude;
        // 终点坐标
        String to = endLatitude + "," + endLongitude;
        String url = String.format(tencentMapProperties.getDistanceUrl(), mode, from, to, tencentMapProperties.getKey());
        // 发送请求
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
        AssertUtil.isEquals(responseEntity.getStatusCodeValue(), HttpStatus.OK.value(), "腾讯地图测距失败");
        // 获取结果
        String body = responseEntity.getBody();
        log.info("t腾讯地图测距结果====={}", body);
        // 将body转换为JSON对象
        JSONObject jsonObject = JSON.parseObject(body);
        // 获取result
        JSONObject resultJsonObject = jsonObject.getJSONObject("result");
        // 获取rows
        JSONArray rowsJsonArray = resultJsonObject.getJSONArray("rows");
        // 获取rows数组第一个元素
        JSONObject rowJsonObject = rowsJsonArray.getJSONObject(0);
        // 获取elements
        JSONArray elementsjsonArray = rowJsonObject.getJSONArray("elements");
        // 获取elements数组第一个元素
        JSONObject elementJsonObject = elementsjsonArray.getJSONObject(0);
        // 获取距离distance
        BigDecimal distance = elementJsonObject.getBigDecimal("distance")
                .divide(new BigDecimal(1000)).setScale(2, RoundingMode.HALF_UP);
        // 获取时长duration
        Long duration = elementJsonObject.getLong("duration") / 60L;
        log.info("t腾讯地图测距结果的距离和时长====={}{}", distance,duration);
        return new DistanceMeasurementResult().setDistance(distance).setDuration(duration);
    }
}
